<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用键名比较计算数组的交集</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.array-intersect-assoc.html">« array_intersect_assoc</a></li>
      <li style="float: right;"><a href="function.array-intersect-uassoc.html">array_intersect_uassoc »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>使用键名比较计算数组的交集</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.array-intersect-key" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_intersect_key</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_intersect_key</span> &mdash; <span class="dc-title">使用键名比较计算数组的交集</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-intersect-key-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_intersect_key</strong></span>(<span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">...$arrays</code></span>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>array_intersect_key()</strong></span> 返回一个数组，该数组包含了所有出现在
   <code class="parameter">array</code> 和其它参数数组中同时存在的键名的值。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.array-intersect-key-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       要检查的数组，作为主值。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">arrays</code></dt>

     <dd>

      <p class="para">
       要被对比的数组。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-intersect-key-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个关联数组，该数组包含了所有出现在
   <code class="parameter">array</code> 和其它参数数组中同时存在的键名的值。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-intersect-key-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
  <td>8.0.0</td>
  <td>
  现在可以仅使用一个参数调用此函数。以前，至少需要两个参数。
  </td>
</tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.array-intersect-key-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4917">
    <p><strong>示例 #1 <span class="function"><strong>array_intersect_key()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array1&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'blue'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'red'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'green'&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'purple'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'green'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'blue'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'yellow'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'cyan'&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_intersect_key</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [&quot;blue&quot;]=&gt;
  int(1)
  [&quot;green&quot;]=&gt;
  int(3)
}
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   上例中可以看到，只有 <code class="literal">&#039;blue&#039;</code>
   和 <code class="literal">&#039;green&#039;</code> 两个键名同时出现在两个数组中，因此被返回。另外注意
   <code class="literal">&#039;blue&#039;</code> 和 <code class="literal">&#039;green&#039;</code>
   的值在两个数组中是不同的。但因为只检查键名，因此还是匹配。返回的只是
   <code class="parameter">array</code> 中的值。
  </p>
  <p class="para">
   在 <code class="literal">key =&gt; value</code> 对中的两个键名仅在
   <code class="literal">(string) $key1 === (string) $key2 </code>
   时才被认为相等。换句话说，执行了严格的类型检查，因此字符串的表达形式必须相同。
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-intersect-key-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-diff.html" class="function" rel="rdfs-seeAlso">array_diff()</a> - 计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="function.array-udiff.html" class="function" rel="rdfs-seeAlso">array_udiff()</a> - 用回调函数比较数据来计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="function.array-diff-assoc.html" class="function" rel="rdfs-seeAlso">array_diff_assoc()</a> - 带索引检查计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="function.array-diff-uassoc.html" class="function" rel="rdfs-seeAlso">array_diff_uassoc()</a> - 用用户提供的回调函数做索引检查来计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="function.array-udiff-assoc.html" class="function" rel="rdfs-seeAlso">array_udiff_assoc()</a> - 带索引检查计算数组的差集，用回调函数比较数据</span></li>
    <li class="member"><span class="function"><a href="function.array-udiff-uassoc.html" class="function" rel="rdfs-seeAlso">array_udiff_uassoc()</a> - 带索引检查计算数组的差集，用回调函数比较数据和索引</span></li>
    <li class="member"><span class="function"><a href="function.array-diff-key.html" class="function" rel="rdfs-seeAlso">array_diff_key()</a> - 使用键名比较计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="function.array-diff-ukey.html" class="function" rel="rdfs-seeAlso">array_diff_ukey()</a> - 用回调函数对键名比较计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="function.array-intersect.html" class="function" rel="rdfs-seeAlso">array_intersect()</a> - 计算数组的交集</span></li>
    <li class="member"><span class="function"><a href="function.array-intersect-assoc.html" class="function" rel="rdfs-seeAlso">array_intersect_assoc()</a> - 带索引检查计算数组的交集</span></li>
    <li class="member"><span class="function"><a href="function.array-intersect-uassoc.html" class="function" rel="rdfs-seeAlso">array_intersect_uassoc()</a> - 带索引检查计算数组的交集，用回调函数比较索引</span></li>
    <li class="member"><span class="function"><a href="function.array-intersect-ukey.html" class="function" rel="rdfs-seeAlso">array_intersect_ukey()</a> - 在键名上使用回调函数来比较计算数组的交集</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>